<?php
namespace Hawk\Controller;
use Hawk\Model;
use \Hawk\Controller\IndexController;
class RoleController extends IndexController 
{
    /**
     * 角色添加
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
    public function add()
    {
    	if(IS_POST)
    	{
    		$model = D('Hawk/Role');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
                    $content = "成功添加角色".$_POST['role_name'];
                    hawk_operation_log(session('id'),hawk_get_ip(),$content);
    				$this->success('添加成功！', U('lst?p='.I('get.p')));
    				exit;
    			}else{
                    $content = "添加角色".$_POST['role_name']."失败";
                    hawk_operation_log(session('id'),hawk_get_ip(),$content);
                }
    		}
    		$this->error($model->getError());
    	}
        //查询权限
        $pmodel = D('Privilege');
        $arr=$pmodel ->getTree();
        $this->assign('arr',$arr);
        //查询角色
        $rmodel = D('Role');
        $role = $rmodel->select();
        $this->assign('role',$role);
		$this->setPageBtn('添加角色', '角色列表', U('lst?p='.I('get.p')));
		$this->display();
    }
    /**
     * 角色修改
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
    public function edit()
    {
    	$id = I('get.id');
    	if(IS_POST)
    	{
    		$model = D('Hawk/Role');
    		if($model->create(I('post.'), 2))
    		{ 
                $role = $model->find($id);
    			if($model->save() !== FALSE)
    			{
                    $content = "成功修改角色".$role['role_name'];
                    hawk_operation_log(session('id'),hawk_get_ip(),$content);
    				$this->success('修改成功！', U('lst', array('p' => I('get.p', 1))));
    				exit;
    			}else{
                    $content = "修改角色".$role['role_name']."失败";
                    hawk_operation_log(session('id'),hawk_get_ip(),$content);
                }
    		}
    		$this->error($model->getError());
    	}
    	$model = M('Role');
    	$data = $model->find($id);
    	$this->assign('data', $data);
        //查询权限
        $pmodel = D('Privilege');
        $arr = $pmodel->getTree();
        //查询当前角色所拥有的权限
        $rpmdoel = M('RolePrivilege');
        $rpdata = $rpmdoel->where('role_id='.$id)->select();
        //二维数组变成一维数组
        $rparr = array();
        foreach($rpdata as $v){
            $rparr[] = $v['pri_id'];
        }
        $this->assign('rparr',$rparr);
        $this->assign('arr',$arr);
        //查询角色
        $rmodel = D('Role');
        $role = $rmodel->select();
        $this->assign('role',$role);
		$this->setPageBtn('修改', '列表', U('lst?p='.I('get.p')));
		$this->display();
    }
    /**
     * 角色删除
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
    public function delete()
    {
    	$model = D('Hawk/Role');
        $role = $model->find($id);
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
            $content = "成功删除角色".$role['role_name'];
            hawk_operation_log(session('id'),hawk_get_ip(),$content);
    		$this->success('删除成功！', U('lst', array('p' => I('get.p', 1))));
    		exit;
    	}
    	else 
    	{
            $content = "删除角色".$role['role_name']."失败";
            hawk_operation_log(session('id'),hawk_get_ip(),$content);
    		$this->error($model->getError());
    	}
    }
    /**
     * 角色列表
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
    public function lst()
    {
    	$model = D('Hawk/Role');
    	$data = $model->search();
        $roleArr = array();
        foreach($data['data'] as $k=>$v){
            $roleArr[$v['role_name']][] = $v;
        }
        //hecho($roleArr);die();
    	$this->assign(array(
    		'data' => $roleArr,
    		'page' => $data['page'],
    	));

		$this->setPageBtn('列表', '添加', U('add'));
    	$this->display();
    }
}